import { userBaseInfo, securityDetail, updatePwd, updateUserBaseInfo } from '@/services/more';

export default {
  state: {
    infoList: {},
    securityList: [],
  },
  effects: {
    // 查询用户基本信息
    *fetchUserInfo({ payload, callback }, { call, put }) {
      const response = yield call(userBaseInfo, payload);

      if (response.success) {
        if (callback) {
          callback(response.data);
        }
        yield put({
          type: 'getUserInfo',
          payload: {
            infoList: response.data,
          },
        });
      }
    },
    // 安全设置回显
    *securityDetail({ payload }, { call, put }) {
      const response = yield call(securityDetail, payload);
      if (response.success) {
        yield put({
          type: 'getSecurityDetail',
          payload: {
            securityList: response.data,
          },
        });
      }
    },
    // 修改密码
    *updatePwd({ payload, callback }, { call }) {
      const response = yield call(updatePwd, payload);
      if (callback) {
        callback(response);
      }
    },
    // 更新信息
    *updateUserBaseInfo({ payload, callback }, { call }) {
      const response = yield call(updateUserBaseInfo, payload);
      if (callback) {
        callback(response);
      }
    },
  },

  reducers: {
    // 查询用户基本信息
    getUserInfo(state, { payload }) {
      return {
        ...state,
        infoList: payload.infoList,
      };
    },
    getSecurityDetail(state, { payload }) {
      return {
        ...state,
        securityList: payload.securityList,
      };
    },
  },
};
